package ru.lenwp.operation.admin;

import ru.lenwp.util.ConsoleUtil;
import ru.lenwp.util.OperationUtil;
import ru.lenwp.context.Context;
import ru.lenwp.model.Account;
import ru.lenwp.operation.Operation;
import ru.lenwp.operation.OperationService;

import java.util.*;

/**
 * Created by Lenar on 27.06.2014.
 */
public class AdminOperationService implements OperationService {

    private final Context context;

    private final Map<Integer, Operation> operationMap;

    public AdminOperationService(Context context) {
        this.context = context;
        operationMap = new LinkedHashMap<Integer, Operation>();
        operationMap.put(1, new ShowUsersOperation(context));
        operationMap.put(2, new BlockUserOperation(context));
        operationMap.put(3, new SaveUsersOperation(context));
        operationMap.put(4, new LoadUsersOperation(context));
        operationMap.put(5, new CreateNewUserOperation(context));
        operationMap.put(6, new EditUserOperation(context));
    }

    public void runService() {
        OperationUtil.showOperationList(operationMap);
        int operationNumber = 0;
        do {
            System.out.println("Выбирите операцию (0 - Выход):");
            operationNumber = ConsoleUtil.askMenuNumber();
            if (operationNumber != 0) {
                Operation operation = operationMap.get(operationNumber);
                if (operation != null) {
                    operation.execute();
                } else {
                    System.out.println("Неверно выбрана операция");
                }
                OperationUtil.showOperationList(operationMap);
            }
        } while (operationNumber != 0);
    }
}
